package com.qc.ayouth.shop.admin.service.user.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qc.ayouth.shop.admin.service.user.AdminAuthService;
import com.qc.ayouth.shop.core.beans.bo.ServiceResultBO;
import com.qc.ayouth.shop.dbs.entity.TSystemAdmin;
import com.qc.ayouth.shop.dbs.entity.TSystemMenu;
import com.qc.ayouth.shop.dbs.entity.TSystemRole;
import com.qc.ayouth.shop.dbs.service.system.TSystemMenuService;
import com.qc.ayouth.shop.dbs.service.system.TSystemRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Haojg on 2020/7/6
 */
@Service
public class AdminAuthServiceImpl implements AdminAuthService {
    @Autowired
    TSystemRoleService tSystemRoleService;
    @Autowired
    TSystemMenuService tSystemMenuService;

    @Override
    public ServiceResultBO getAdminAuthInfo(TSystemAdmin adminInfo) {
        ServiceResultBO bo = new ServiceResultBO();

//        {
//                'id': '4291d7da9005377ec9aec4a71ea837f',
//                'name': '天野远子',
//                'username': 'admin',
//                'password': '',
//                'avatar': '/avatar2.jpg',
//                'status': 1,
//                'telephone': '',
//                'lastLoginIp': '27.154.74.117',
//                'lastLoginTime': 1534837621348,
//                'creatorId': 'admin',
//                'createTime': 1497160610259,
//                'merchantCode': 'TLif2btpzg079h15bk',
//                'deleted': 0,
//                'roleId': 'admin',
//                'role': {}
//        }
        JSONObject userInfo = new JSONObject();
        userInfo.put("name", adminInfo.getName());
        userInfo.put("username", adminInfo.getUsername());
        userInfo.put("avatar", adminInfo.getAvatar());

        // 1.获取角色信息
        List<TSystemRole> roleList = tSystemRoleService.getRoleListByIdList(adminInfo.getRoleList());
        if (roleList.size() <= 0) {
            bo.fail("当前用户暂无角色信息");
            return bo;
        }
        TSystemRole systemRole = roleList.get(0);
        userInfo.put("roleId", systemRole.getRoleName());

        JSONObject roleInfo = new JSONObject();
        roleInfo.put("name", systemRole.getRoleName());
        // 角色标识
        roleInfo.put("roleSign", systemRole.getRoleSign());

        // 2.获取角色对应的目录和菜单权限
        JSONArray rolePermissions = new JSONArray();
        List<TSystemMenu> menuList = tSystemMenuService.getMenuListByRoleIds(adminInfo.getRoleList());
        for (TSystemMenu menu : menuList) {
            // 1.保存数据
            JSONObject permissionItem = new JSONObject();
            permissionItem.put("id", menu.getId());
            permissionItem.put("permissionId", menu.getPermission());
            permissionItem.put("permissionName", menu.getMenuTitle());

            // 2.DB获取每一级菜单对应的actions
            JSONArray actionArray = new JSONArray();
            List<TSystemMenu>  actionList = tSystemMenuService.getActionListByMenuId(menu.getId());
            for (TSystemMenu action : actionList) {
                JSONObject actionItem = new JSONObject();
                actionItem.put("id", action.getId());
                actionItem.put("describe", action.getMenuTitle());
                actionItem.put("action", action.getPermission());
                actionArray.add(actionItem);
            }
            permissionItem.put("actionEntitySet", actionArray);

            // 3.添加数据
            rolePermissions.add(permissionItem);
        }

        roleInfo.put("permissions", rolePermissions);
        userInfo.put("role", roleInfo);
        // result: vue格式这样，就不做修改了
        bo.add("userAuthInfo", userInfo);
        bo.success();
        return bo;
    }
}
